package com
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class SimpleLoadQueue extends EventDispatcher
	{
		public static const ALL_LOADED:String = "allLoaded";
		private var _pathList:Array;
		private var _totalNum:int=0;
		private var _isLoading:Boolean;
		private var _currentPath:String;
		private var _currentFileName:String;
		
		private var _urlLoader:URLLoader;
		public var fileDic:Dictionary;
		public function SimpleLoadQueue()
		{
			_pathList =[];
			fileDic = new Dictionary();
			
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
		}
		
		public function addPath(fileObj:Object):void
		{
			_pathList.push(fileObj);
			_totalNum++;
		}
		
		public function load():void
		{
			if(_pathList.length==0){
				_isLoading=false;
				this.dispatchEvent(new Event(SimpleLoadQueue.ALL_LOADED));
				return;
			}
			
			if(_isLoading==false){
				_isLoading = true;
				var currentFile:Object =  _pathList.shift();
				_currentPath = currentFile.path;
				_currentFileName = currentFile.fileName;
				
				_urlLoader.addEventListener(Event.COMPLETE,onFileLoadedHandle);
				_urlLoader.load(new URLRequest(_currentPath));
			}
		}
		
		private function onFileLoadedHandle(event:Event):void
		{
			var targetLoader:URLLoader = event.target as URLLoader;
			targetLoader.removeEventListener(Event.COMPLETE,onFileLoadedHandle);
			fileDic[_currentFileName] = targetLoader.data as ByteArray;
			_isLoading=false;
			this.dispatchEvent(new DataEvent(DataEvent.DATA,false,false,_currentPath));
			load();
		}
	}
}